package com.hex.ds.em.hcdp.gauss.analyze.script.performance;

import static com.hex.ds.em.hcdp.gauss.pack.service.impl.ScriptPackTaskServiceImpl.wordResult;

import com.hex.ds.em.hcdp.basic.common.IResultForReport;
import com.hex.ds.em.hcdp.gauss.analyze.script.performance.entity.CheckOrLogicOperatorEntity;
import com.hex.ds.em.hcdp.gauss.common.entity.ScriptPackScanResultEntity;
import com.hex.ds.em.hcdp.gauss.common.enums.AntlrEnum;
import com.hex.hdtp.common.utils.AntlrUtil;
import com.hex.hlrt.gauss.g4.basic.gen.GaussParser;
import com.hex.hlrt.gauss.g4.basic.gen.GaussParser.Logical_expressionContext;
import com.hex.hlrt.gauss.g4.basic.gen.GaussParser.Relational_expressionContext;
import com.hex.hlrt.gauss.g4.basic.gen.GaussParserBaseListener;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

/**
 * @Package com.hex.ds.em.hcdp.gauss.analyze.script.orConditionCheck
 * @ClassName orConditionCheckListener
 * @Description 检查or条件是否可以用in替换
 * @Author gj.xu
 * @Date 2024/9/13 18:45
 * @Version v2.0
 **/
@Slf4j
@Accessors(chain = true)
@Scope(value = "prototype")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service("Gauss-CheckOrLogicOperator")
public class CheckOrLogicOperatorListener extends GaussParserBaseListener implements
    IResultForReport {

  @Override
  public void exitLogical_expression(GaussParser.Logical_expressionContext ctx) {
    if (ctx.OR() != null) {
      List<Logical_expressionContext> logical_expressionContexts = ctx.logical_expression();

      //获取OR前表达式的内容
      Relational_expressionContext relational_expressionContext1 = (Relational_expressionContext) AntlrUtil
          .queryDesignateChildrenOfSumObj(logical_expressionContexts.get(0),
              Relational_expressionContext.class).get(0);
      String finalText1 = relational_expressionContext1.getText().trim();
      if (relational_expressionContext1.relational_expression().size() == 2) {
        String discardedText1 = relational_expressionContext1.relational_expression(1).getText()
            .trim();
        finalText1 = finalText1.replace(discardedText1, "").trim();
      }
      if (relational_expressionContext1.compound_expression() != null) {
        if (relational_expressionContext1.compound_expression().in_elements() != null) {
          String discardedText1 = relational_expressionContext1.compound_expression()
              .in_elements()
              .getText().trim();
          finalText1 = finalText1.replace(discardedText1, "").trim();
        }else if(relational_expressionContext1.compound_expression().between_elements()!=null){
          String discardedText1 = relational_expressionContext1.compound_expression()
              .between_elements()
              .getText().trim();
          finalText1 = finalText1.replace(discardedText1, "").trim();
        }else if(relational_expressionContext1.compound_expression().concatenation(1)!=null){
          String discardedText1 = relational_expressionContext1.compound_expression()
              .concatenation(1)
              .getText().trim();
          finalText1 = finalText1.replace(discardedText1, "").trim();
        }
      }
      //获取OR后表达式的内容
      Relational_expressionContext relational_expressionContext2 = (Relational_expressionContext) AntlrUtil
          .queryDesignateChildrenOfSumObj(logical_expressionContexts.get(1),
              Relational_expressionContext.class).get(0);
      String finalText2 = relational_expressionContext2.getText().trim();
      if (relational_expressionContext2.relational_expression().size() == 2) {
        String discardedText2 = relational_expressionContext2.relational_expression(1).getText()
            .trim();
        finalText2 = finalText2.replace(discardedText2, "").trim();
      }
      if (relational_expressionContext2.compound_expression() != null) {
        if (relational_expressionContext2.compound_expression().in_elements() != null) {
          String discardedText2 = relational_expressionContext2.compound_expression()
              .in_elements()
              .getText().trim();
          finalText2 = finalText2.replace(discardedText2, "").trim();
        }else if(relational_expressionContext2.compound_expression().between_elements()!=null){
          String discardedText2 = relational_expressionContext2.compound_expression()
              .between_elements()
              .getText().trim();
          finalText2 = finalText2.replace(discardedText2, "").trim();
        }else if(relational_expressionContext2.compound_expression().concatenation(1)!=null){
          String discardedText2 = relational_expressionContext2.compound_expression()
              .concatenation(1)
              .getText().trim();
          finalText2 = finalText2.replace(discardedText2, "").trim();
        }
      }

      if (finalText1.equalsIgnoreCase(finalText2)) {
        // 更新扫描后word指标结果
        setResultForWordReport();

        CheckOrLogicOperatorEntity ruleEntity = new CheckOrLogicOperatorEntity();
        ctx.addHeadAnyChild(ruleEntity);
        log.info("rule-[CheckOrLogicOperator]:");
      }
    }
  }

  @Override
  public void setResultForWordReport() {
    ScriptPackScanResultEntity result = wordResult.get(AntlrEnum.OR_CONDITION_CHECK.getValue());
    if (result == null) {
      return;
    }
    result.setScanTable("").setScanState("fail");
    wordResult.put(AntlrEnum.OR_CONDITION_CHECK.getValue(), result);
  }
}
